package com.farm.forms.service.impl;

import java.util.ArrayList;
import com.farm.forms.domain.Formcategory;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import com.farm.forms.dao.FormcategoryDaoInter;
import com.farm.forms.service.FormcategoryServiceInter;

/* *
 *功能：表单分类服务实现类
 *详细：
 *
 *版本：v0.1
 *作者：Farm代码工程自动生成
 *日期：20141122211253
 *说明：
 */
public class FormcategoryServiceImpl implements FormcategoryServiceInter {
	private FormcategoryDaoInter formcategoryDao;

	@Override
	public Formcategory insertFormcategoryEntity(Formcategory entity,
			LoginUser user) {
		entity.setCuser(user.getId());
		entity.setCtime(TimeTool.getTimeDate14());
		entity.setCusername(user.getName());
		entity.setEuser(user.getId());
		entity.setEusername(user.getName());
		entity.setEtime(TimeTool.getTimeDate14());
		entity.setPstate("1");
		if (entity.getParentid() == null) {
			entity.setParentid("NONE");
		}
		entity.setTreecode("NONE");
		entity = formcategoryDao.insertEntity(entity);
		if (entity.getParentid().equals("NONE")) {
			entity.setTreecode(entity.getId());
		} else {
			entity.setTreecode(formcategoryDao.getEntity(entity.getParentid())
					.getTreecode()
					+ entity.getId());
		}
		formcategoryDao.editEntity(entity);
		return entity;
	}

	@Override
	public Formcategory editFormcategoryEntity(Formcategory entity,
			LoginUser user) {
		Formcategory entity2 = formcategoryDao.getEntity(entity.getId());
		// entity2.setEuser(user.getId());
		// entity2.setEusername(user.getName());
		// entity2.setEtime(TimeTool.getTimeDate14());
		// entity2.setTreecode(entity.getTreecode());
		entity2.setName(entity.getName());
		entity2.setSort(entity.getSort());
		// entity2.setParentid(entity.getParentid());
		entity2.setPcontent(entity.getPcontent());
		// entity2.setPstate(entity.getPstate());
		// entity2.setEtime(entity.getEtime());
		// entity2.setCusername(entity.getCusername());
		// entity2.setCuser(entity.getCuser());
		// entity2.setEusername(entity.getEusername());
		// entity2.setEuser(entity.getEuser());
		// entity2.setId(entity.getId());
		// entity2.setCtime(entity.getCtime());
		formcategoryDao.editEntity(entity2);
		return entity2;
	}

	@Override
	public void deleteFormcategoryEntity(String id, LoginUser user) {
		// TODO 自动生成代码,修改后请去除本注释
		if (formcategoryDao.selectEntitys(
				DBRule.addRule(new ArrayList<DBRule>(), "parentid", id, "="))
				.size() > 0) {
			throw new RuntimeException("不能删除该节点，请先删除其子节点");
		}
		formcategoryDao.deleteEntity(formcategoryDao.getEntity(id));
	}

	@Override
	public Formcategory getFormcategoryEntity(String id) {
		// TODO 自动生成代码,修改后请去除本注释
		if (id == null) {
			return null;
		}
		return formcategoryDao.getEntity(id);
	}

	@Override
	public DataQuery createFormcategorySimpleQuery(DataQuery query) {
		// TODO 自动生成代码,修改后请去除本注释
		DataQuery dbQuery = DataQuery
				.init(
						query,
						"FARM_FORMS_CATEGORY",
						"ID,TREECODE,NAME,SORT,PARENTID,PCONTENT,PSTATE,ETIME,CUSERNAME,CUSER,EUSERNAME,EUSER,CTIME");
		return dbQuery;
	}

	// ----------------------------------------------------------------------------------
	public FormcategoryDaoInter getFormcategoryDao() {
		return formcategoryDao;
	}

	public void setFormcategoryDao(FormcategoryDaoInter dao) {
		this.formcategoryDao = dao;
	}

}
